package com.lantosec.hmrp.modules.eval.controller;


import com.lantosec.hmrp.core.kit.PageVO;
import com.lantosec.hmrp.core.kit.tree.TreeNodeVo;
import com.lantosec.hmrp.core.log.ControllerLog;
import com.lantosec.hmrp.core.log.LogType;
import com.lantosec.hmrp.core.util.DateUtils;
import com.lantosec.hmrp.modules.eval.entity.EvalSchemeList;
import com.lantosec.hmrp.modules.eval.entity.EvalType;
import com.lantosec.hmrp.modules.eval.vo.EvalItemVO;
import com.lantosec.hmrp.modules.eval.vo.EvalTypeVO;
import org.springframework.web.bind.annotation.RequestMapping;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import com.lantosec.hmrp.core.kit.Result;
import com.lantosec.hmrp.modules.eval.entity.EvalScheme;
import com.lantosec.hmrp.modules.eval.service.IEvalSchemeService;
import com.lantosec.hmrp.modules.eval.vo.EvalSchemeVO;

import java.util.Arrays;
import java.util.List;
import lombok.extern.log4j.Log4j2;
import org.springframework.web.bind.annotation.RestController;
import com.lantosec.hmrp.core.mvc.BaseController;

/**
 *
 * 考评方案 控制器
 *
 * @author AutoCode
 * @since 2020-03-16
 */
@Api(value = "考评方案控制器", tags = "考评方案控制器")
@RestController
@RequestMapping("/eval/scheme")
@Log4j2
public class EvalSchemeController extends BaseController {
    @Autowired
    private IEvalSchemeService service;

    @GetMapping("queryAll/{evalSchemeId}")
    @ApiOperation(value = "编辑时查考评方案信息")
    public Result<EvalSchemeList> queryAll(@ApiParam(value = "evalSchemeId", required = true) @PathVariable(name="evalSchemeId") Integer evalSchemeId){
        EvalSchemeList evalSchemeList = service.getEvalSchemeAll(evalSchemeId);
        return Result.ok(evalSchemeList);
    }

    @GetMapping("page")
    @ApiOperation(value = "分页查询")
    public Result<Page<EvalSchemeVO>> page(@ApiParam(value = "分页对象", required = true) PageVO<EvalScheme> page){
        Page<EvalSchemeVO> pageVO = service.pageAll(page);
        return Result.ok(pageVO);
    }

    @GetMapping("get/{evalSchemeId}")
    @ApiOperation(value = "查询考评方案详情")
    public Result<List<EvalItemVO>> get(@ApiParam(value = "evalSchemeId", required = true) @PathVariable(name="evalSchemeId") Integer evalSchemeId){
        List<EvalItemVO> evalItemVO = service.getEvalSchemeParm(evalSchemeId);
        return Result.ok(evalItemVO);
    }

    @GetMapping("getDeptUserTree")
    @ApiOperation(value = "查询部门人员树")
    public Result<List<TreeNodeVo>> getDeptUserTree(){
        List<TreeNodeVo> deptUserTree = service.getDeptUserTree();
        return Result.ok(deptUserTree);
    }

    @GetMapping("deptUserTree")
    @ApiOperation(value = "查询部门人员树（只针对于新增考评项目返回类型是deptid+userid）")
    public Result<List<TreeNodeVo>> DeptUserTree(){
        List<TreeNodeVo> deptUserTree = service.deptUserTree();
        return Result.ok(deptUserTree);
    }

    @GetMapping("getDeptPositionTree")
    @ApiOperation(value = "查询部门岗位树")
    public Result<List<TreeNodeVo>> getDeptPositionTree(){
        List<TreeNodeVo> deptPositionTree = service.getDeptPositionTree();
        return Result.ok(deptPositionTree);
    }

    @GetMapping("getEvalSchemeAll")
    @ApiOperation(value = "查询所有的考评方案")
    public Result<List<EvalSchemeVO>> getEvalSchemeAll(){
        List<EvalSchemeVO> evalSchemeVOList = service.getEvalScheme();
        return Result.ok(evalSchemeVOList);
    }

    @PostMapping("create")
    @ControllerLog(name = "新增考评方案", type = LogType.add)
    @ApiOperation(value = "新增考评方案")
    public Result<Integer> create(@ApiParam(value = "考评方案封装对象", required = true) @RequestBody @Validated EvalSchemeList evalSchemeList){
        Integer evalSchemeId = service.saveEvalScheme(evalSchemeList);
        return Result.ok(evalSchemeId);
    }

    @PutMapping("update")
    @ControllerLog(name = "修改考评方案", type = LogType.update)
    @ApiOperation(value = "修改数据")
    public Result<Void> update(@ApiParam(value = "考评方案封装对象", required = true) @RequestBody EvalSchemeList evalSchemeList){
        service.updateEvalScheme(evalSchemeList);
        return Result.ok();
    }

    @DeleteMapping(value = "delete/{ids}")
    @ControllerLog(name = "逻辑删除考评方案", type = LogType.delete)
    @ApiOperation("逻辑删除")
    public Result<Void> delete(@ApiParam(value = "ID列表,用逗号分隔", required = true) @PathVariable(name="ids") String ids){
        if(StringUtils.isNotEmpty(ids)){
            service.removeByIds(Arrays.asList(ids.split(",")));
        }
        return Result.ok();
    }
}
